package com.example.RemindMe;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;

public class SettingsActivity extends Activity {
	/** Called when the activity is first created. */

	private String uriSelected;
	SharedPreferences sharedPreferences;
	Button btnRingtone;
	Spinner spn;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setRequestedOrientation(1);
		setContentView(R.layout.activity_settings);
		sharedPreferences = getSharedPreferences("settingstab", 0);
		btnRingtone = (Button) findViewById(R.id.button2);

		String ringtoneTitle = sharedPreferences.getString("titlealarm", "0");
		String snoozeDauer = sharedPreferences.getString("snooze", "0");
		btnRingtone = (Button) findViewById(R.id.button2);
		btnRingtone.setText(ringtoneTitle);
		spn = (Spinner) findViewById(R.id.spinner2);
		spn.setSelection(Integer.parseInt(snoozeDauer));

		spn.setOnItemSelectedListener(new OnItemSelectedListener() {

			/**
			 * 
			 * Setzen der Snooze-Dauer
			 * 
			 */
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				Editor editor = sharedPreferences.edit();
				editor.putString("snooze", "" + arg0.getSelectedItemId());

				editor.commit();

			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});

	}

	/**
	 * 
	 * RingtonePicker
	 * 
	 */
	public void setDefaultRingtone(View v) {

		Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
				"Select ringtone for notifications:");
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
				RingtoneManager.TYPE_NOTIFICATION);

		this.startActivityForResult(intent, 999);

	}

	/**
	 * 
	 * Speichern des Default-Alarmtons in einer SharedPreference
	 * 
	 */
	protected void onActivityResult(int requestCode, int resultCode,
			Intent Mringtone) {
		switch (resultCode) {

		case RESULT_OK:

			Editor editor = sharedPreferences.edit();

			Uri uri = Mringtone
					.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			Ringtone titel = RingtoneManager.getRingtone(this, uri);
			uriSelected = uri.toString();
			editor.putString("titlealarm", titel.getTitle(this));
			editor.putString("urialarm", uriSelected);
			editor.commit();
			btnRingtone.setText(titel.getTitle(this));

			break;
		}

	}



    //------------------------------------------------Action Bar Methods -----------------------------------------------
    public void goHome(Context context)
    {
        final Intent intent = new Intent(context, HomeActivity.class);
        intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity (intent);


        //geändert
        finish();
    }


    public void onClickHome (View v)
    {
        goHome (this);
    }

    /**
     * Handle the click on the search button.
     *
     * @param v View
     * @return void
     */

    public void onClickSearch (View v)
    {
        startActivity (new Intent(getApplicationContext(), SearchActivity.class));

        //geändert
        finish();

    }

    /**
     * Handle the click on the About button.
     *
     * @param v View
     * @return void
     */

    public void onClickAbout (View v)
    {
        startActivity (new Intent(getApplicationContext(), AboutActivity.class));

        //geändert
        finish();

    }

    /**
     * Handle the click of a Feature button.
     *
     * @param v View
     * @return void
     */

    /**
     * Use the activity label to set the text in the activity's title text view.
     * The argument gives the name of the view.
     *
     * <p> This method is needed because we have a custom title bar rather than the default Android title bar.
     * See the theme definitons in styles.xml.
     *
     * @param textViewId int
     * @return void
     */

    public void setTitleFromActivityLabel (int textViewId)
    {
        TextView tv = (TextView) findViewById (textViewId);
        if (tv != null) tv.setText (getTitle ());
    } // end setTitleText

    /**
     * Show a string on the screen via Toast.
     *
     * @param msg String
     * @return void
     */

    public void toast (String msg)
    {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
    } // end toast

    /**
     * Send a message to the debug log and display it using Toast.
     */
    public void trace (String msg)
    {
        Log.d("Demo", msg);
        toast (msg);
    }

}